<?php
session_start();
require 'common/Utility.php';
require 'db/configuration.php';
require 'crud/CustomSecurity.class.php';
require 'crud/Users.class.php';
require 'crud/Userdata.class.php';
require 'crud/Countries.class.php';
require 'crud/Players.class.php';
require 'crud/Matches.class.php';
require 'crud/Prognosis.class.php';
include_once("js/ajax/agent.php");
loadDB();
loginCheck();
$agent->init();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<link rel="stylesheet" type="text/css" href="css/page.css" />
<link rel="stylesheet" type="text/css" href="css/jquery-ui-1.8.1.custom.css" />
<link rel="stylesheet" type="text/css" href="css/component-product.css" />
<link rel="stylesheet" type="text/css" href="js/fancybox/jquery.fancybox-1.2.5.css" media="screen"/>
<link rel="stylesheet" type="text/css" href="css/tipTip.css" />
<link rel="stylesheet" type="text/css" href="css/testing.css" />

<script type="text/javascript" src="js/jquery-1.4.2.min.js"> </script>
<script type="text/javascript" src="js/jquery.ui.core.js"> </script>
<script type="text/javascript" src="js/jquery.easing.1.3.js"> </script>
<script type="text/javascript" src="js/fancybox/jquery.fancybox-1.2.5.pack.js"></script>
<script type="text/javascript" src="js/util.js"></script>
<script type="text/javascript" src="js/debug.js"></script>
<script type="text/javascript" src="js/betmatches.js"></script>
<script type="text/javascript" src="js/jedit/jquery.jeditable.mini.js"> </script>
<script type="text/javascript" src="js/jquery-ui-1.8.1.custom.min.js"> </script>
<script type="text/javascript" src="js/quicksand/jquery.quicksand.js"> </script>
<script type="text/javascript" src="js/quicksand/jquery-animate-css-rotate-scale.js"> </script>
<script type="text/javascript" src="js/jquery.tipTip.js"> </script>
<script type="text/javascript" src="js/jedit/jquery.jeditable.mini.js"></script>
<script type="text/javascript" src="js/iscroll.js"></script>

<link href=' http://fonts.googleapis.com/css?family=Droid+Serif' rel='stylesheet' type='text/css'>

<title>World Cup 2010 - игРъТъ</title>
</head>
<body>

<?php include("common/lenta.php"); ?>

<div id="main">
	<div id="logo-text">World Cup 2010</div>
	<div id="product-view">
	<div id="top"><!-- <h2>Прогнози</h2> -->
		
	</div>

	<div id="content">
		
		<div id="tabs">
			<ul>
				<li><a href="#fragment-matches"><span>Прогнози</span></a></li>
				<li><a href="#fragment-groups"><span>Групи</span></a></li>
				<li><a href="#fragment-elimination"><span>Елиминационна фаза</span></a></li>
			</ul>
			<div id="fragment-matches">
				<?php
					$mathces = new Matches(con());
					list_matches($mathces->getAll());
				?>
			</div>
			<div id="fragment-groups">
				<?php
					$headerTitles[0] = "Oтбoр";
					$headerTitles[1] = "Победи";
					$headerTitles[2] = "Загуби";
					$headerTitles[3] = "Равни";
					$headerTitles[4] = "+Голове";
					$headerTitles[5] = "-Голове";
					$headerTitles[6] = "Точки";
					$mathces = new Matches(con());
//					pre_dump($mathces->computeRanking("Group C"));
					list_table_raw_simple($mathces->computeRanking("Group A"), $headerTitles, "Group A");
					list_table_raw_simple($mathces->computeRanking("Group B"), $headerTitles, "Group B");
					list_table_raw_simple($mathces->computeRanking("Group C"), $headerTitles, "Group C");
					list_table_raw_simple($mathces->computeRanking("Group D"), $headerTitles, "Group D");
					list_table_raw_simple($mathces->computeRanking("Group E"), $headerTitles, "Group E");
					list_table_raw_simple($mathces->computeRanking("Group F"), $headerTitles, "Group F");
					list_table_raw_simple($mathces->computeRanking("Group G"), $headerTitles, "Group G");
				?>
			</div>
			<div id="fragment-elimination"><div id="koboard"></div></div>
		</div>

	</div>
	</div>
	<div id="right-column">
	  <div id="product-view2"> 
	    <div id="top2"><h2>Статистика</h2></div>
	    <div id="content2">
		<?php
				$userdataobject = new Userdata(con());
				$userdata = $userdataobject->get();
				$progz = new Prognosis(con());
				$progz->getPoints();
				$points = "няма"; 
				if (count($userdata) > 0){
					$points1 = $progz->getPoints();
					$points = $points1[0][0];
				}
				echo '     Точки: <b>'. $points.'</b><br/>';
			?>
	    <?php 
						$champ = "няма";
						$goalscorer = "няма";
						if (count($userdata) > 0){
							$champ = $userdata[0][1];
							$goalscorer = $userdata[0][2];
						} 
						$classes = 'class="edit_champ"';
							date_default_timezone_set('Europe/Helsinki');		

						if (time() > mktime(14,46,0,6,22,2010)){
							$classes = '';
						}
//						$jsonArray = getCountries();
						$jsonArray = rencode(getCountries());
						echo '<script language="javascript" type="text/javascript" defer> setCountries('.json_encode($jsonArray).');</script>';
						echo 'Потребител: '. $_SESSION['user']. '  <br/> Прогнози за:<br/>шампион:  <b '.$classes.'>'. $champ.'</b>   <br/> гол майстор: <b class="edit_gscr">'.$goalscorer.'</b>';
						
						
    	?>
    	</div>
	  </div>	
	  <div id="product-view1">
		<div id="top1">
		<h2>Класиране:</h2>
		</div>
		<div id="content1">
			<?php 
				echo list_ranking();
//				$mathces = new Matches(con());
//				$results = $mathces->computeRanking("Group A");
//				
//				foreach($results as $result){
//					echo $result[0].$result[1].$result[2];
//				}
			
			?>

		</div>
   	  </div>	
   	  <div id="golmajstori">
		<div id="top3">
		<h2>Голмайстори:</h2>
		</div>
		<div id="content3">
			<?php 
				listGoalScorers();
			?>
		</div>
   	  </div>	
	</div>
	
	<div id="middle-column">
		<iframe width="230" scrolling="no" height="586" frameborder="0" style="border: 0px none transparent;" src="http://www.ustream.tv/socialstream/4600965"></iframe>
	</div>
		
	</div>

			<?php
			function list_matches($table){
				date_default_timezone_set('Europe/Helsinki');
				$currDate = date("Y-m-d");
				echo '<div id="accordion">';
				$datecache = '';
				$started = 0;
				$ctr = 1;
				$indexAcc = 0;
				$archived = 1;
				$round = 1;
				$counter = 0;
				$change = 0;
				foreach($table as $line){
					$counter++;
					$change = 0;
					if ($archived == 0) {
						$round = 0;
					} else if ($counter > 16*$round){
						$round++;
					}
					$id = $line[0];
					$teamA = $line[2];
					$teamB = $line[5];
					$resA = $line[3];
					$resB = $line[4];
					$progA = $line[7];
					$progB = $line[8];
					$group = $line[9];
					$dateTime = $line[1];
					$delim = ':';
					if (strcmp($resA, '-1')==0){
						$resA = '?';
						$resB = '?';
					}
					$dateTimeShort = substr($dateTime,0,10);

					if (strcasecmp($datecache, $dateTimeShort) != 0){
						if ($indexAcc == 0 && (strcasecmp($currDate, $dateTimeShort) == 0 || time() < get_timestamp($dateTimeShort))){
							$archived = 0;
						}
						if($started == 1 && ($archived == 0 || $counter > 16*($round+1)) ){
							echo '</div>';
						}
						echo '<h3><a href="#">';
						if ($archived == 0){
							echo $dateTimeShort;
							if ($indexAcc == 0 && strcasecmp($currDate, $dateTimeShort) == 0){
								$indexAcc = $ctr;
								echo' (ДНЕС)';
							} else if (time() < get_timestamp($dateTimeShort)){
								if ($indexAcc == 0){
								  $indexAcc = $ctr;	
								}
								
							}
						} else {
							if ($started == 0 || $change == 1){
								if ($round == 1){
									echo "архив";
								} else if ($round == 2){
									echo "Втори Кръг (архив)";
								} else if ($round == 3){
									echo "Трети Кръг (архив)";
								}
									
							}

						}
						echo '</a></h3>';
							
						if($started == 0 || $archived == 0){
							echo '<div class="containerResults0">';
						}
						$started = 1;
						$datecache = $dateTimeShort;
						if ($archived == 0){
							$ctr++;
						}
							
					}
	    drawMatchDetails($teamA, $teamB,$resA, $resB, $progA, $progB, $dateTime, $id, $group);
				}
				echo '</div>';
				echo '</div>';
				echo '<script>$(document).ready(function() {$("#accordion").accordion({ autoHeight: false, active: '.$indexAcc.', collapsible: true })});</script>';
			}

function list_ranking(){
	$userdata = new Userdata(con());
	$table = $userdata->getRanking();
	$result = '';
//	pre_dump($table);
	$result =$result.'<ol>';
	foreach($table as $line){
		$result =$result."<li class='tiptip' title='Шампион: $line[2]   Голмайстор: $line[3]'>";
		$result =$result.'			<a href="#" onclick="listVisibleMatches('.$line[4].')">'.$line[0].' - '.$line[1].' точки</a>';
		$result =$result.'</li>';
	}
	$result =$result.'</ol>';
	return $result;
}

function listGoalScorers(){
	$players = new Players(con());
	$table = $players->getTopGoalscorers();
//	pre_dump($table);
	echo '<ul>';
	$position = 1;
	$cahce1 = $table[0][2];
	foreach($table as $line){
		if ($cahce1 != $line[2]){
			$position++;
			$cahce1 = $line[2];
		}
		echo "<li>";
		echo $position.'. '.$line[0].' ('.$line[1].')- '.$line[2].' гола';			
		echo '</li>';
	}
	echo '</ul>';
}
?>
<script type="text/javascript">
$(document).ready(function(){
	 enableEditable3();
	$('a#fAddButton').fancybox({
		'hideOnContentClick': false,
		'showCloseButton':true,
		'frameWidth': 358,
		'frameHeight': 202,
		'padding':0
	});
	resizemain();
	$("#tabs").tabs();
//	document.addEventListener('touchmove', function(e){ e.preventDefault(); });
//	myScroll = new iScroll('fragment-matches');
//	myScroll = new iScroll('fragment-groups');
	 });

</script>

<script>
var t;
  $(document).ready(function() {
//    $("#accordion").accordion({ autoHeight: false, active: <?=$indexAcc?> });
//    $(".tiptip").tipTip();
	  
  });
  $(window).focus(function(){
	  clearTimeout(t);
	  t = setTimeout('check()', 2000);
  });

  function check(){
	  agent.call('','isSessionActive','sessionCallback');;
	}

  function sessionCallback(result){
	  if (result == 1){
	  } else {
		  setTimeout('delayer()', 1000);
	  }
		  
  }
  function delayer(){
//	    window.location = "./login.php";
	  window.location.reload(1);
	}
  </script>
  <?php 
  	function drawMatchDetails($teamA, $teamB,$resA, $resB, $progA, $progB, $date, $id, $group){
  		if ($resA == -1){
  			$resA = '?';
  		}
  		if ($resB == -1){
  			$resB = '?';
  		}
  		if (strcmp($progA,'') == 0){
  			$progA = '?';
  		}
  		if (strcmp($progB,'') == 0){
  			$progB = '?';
  		}
  		echo '<div class="containerResults1">';
		echo '	<div class="tleft">';
	  	echo '		<div class="tleft-in">';
	    echo '			<img id="teamA" class="flags" src="images/flags/'.$teamA.'.png"/>';
	  	echo '		</div>';
	  	echo '		<div class="tright-in">';
	    echo '			<h4>'.$teamA.'</h4>';
	  	echo '		</div>  ';
		echo '	</div>';
		
		echo '	<div class="tcenter">'.$resA.':'.$resB.'</div>';
		
		echo '	<div class="tright">';
		echo '		<div class="tleft-in">';
		echo '			<h4>'.$teamB.'</h4>';
		echo '		</div>';
		echo '		<div class="tright-in">';
		echo '			<img id="teamB" class="flags" src="images/flags/'.$teamB.'.png"/>';
		echo '		</div>';
		echo '	</div>';
		
		echo '	<div class="tbottom">';
		echo '		<div class="tbottom-l">';
		echo "			$date        $group";
		echo '		</div>';
		echo '		<div class="tbottom-r">';
		echo '			<a id=fAddButton class="thickbox" href="matchpredictions.php?iframe&idm='.$id.'">Предположение: '.$progA.':'.$progB.'</a>';
		echo '		</div>';
		echo '	</div>';
		echo '</div>';
  	}
  	
  	function getBetMatches($id){
  		$progs = new Prognosis(con());
        $results = $progs->getUserPrognosis($id);
        return $results;
  	}
  	
  	function getCountries(){
  		$cts = new Countries(con());
    	$aaa = $cts->getAll();
    	return $aaa;
  	}
  ?>


</body>
</html>
